package com.lxw.manage_cms.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxw.framework.domain.cms.CmsPage;
import com.lxw.framework.domain.cms.CmsTemplate;
import com.lxw.framework.web.BaseController;
import com.lxw.manage_cms.service.ICmsGridFsService;
import com.lxw.manage_cms.service.ICmsPageService;
import com.lxw.manage_cms.service.ICmsTemplateService;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("preview")
public class CmsPagePreviewController  extends BaseController {


    @Autowired
    ICmsPageService cmsPageService;

    @Autowired
    ICmsGridFsService cmsGridFsService;

    @Autowired
    ICmsTemplateService cmsTemplateService;
    @Autowired
    RestTemplate restTemplate;

    //显示视图
    @RequestMapping("/show/{pageId}")
    public void showPreview(@PathVariable String pageId, Model model) {
        //获取页面信息
        CmsPage cmsPage = cmsPageService.findById(pageId);
        //通过id查询到绑定的模板信息到储存桶中查询到信息
        String templateId = cmsPage.getTemplateId();
        CmsTemplate cmsTemplate = cmsTemplateService.findTemplateById(templateId);
        String templateFileId = cmsTemplate.getTemplateFileId();
        String tempStr = cmsGridFsService.queryContentById(templateFileId);
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
        //给取出来的字符串设置成模板时起个名字
        String uuid = UUID.randomUUID().toString().replace("-", "");
        stringTemplateLoader.putTemplate(uuid,tempStr);
        //获取model信息
        String dataUrl = cmsPage.getDataUrl();
        String forObject = restTemplate.getForObject(dataUrl, String.class);
        JSONObject jsonObject = JSON.parseObject(forObject);
        model.addAttribute("obj",jsonObject);


        //静态化页面
        Configuration configuration = new Configuration(Configuration.getVersion());
        configuration.setURLEscapingCharset("UTF-8");
        configuration.setTemplateLoader(stringTemplateLoader);
        Template template = null;
        try {
            template = configuration.getTemplate(uuid);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String htmlStr = null;
        try {
            htmlStr = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        } catch (IOException e) {


        } catch (TemplateException e) {
            e.printStackTrace();
        }

        response.setContentType("text/html;charset=utf-8");
        try {
            response.getWriter().write(htmlStr);
            response.getWriter().close();
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
